package com.fdbs.controller;

import com.fdbs.util.FdfsUtils;
import com.fdbs.util.FileUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/up")
@CrossOrigin
public class Controller {

    /**
     * 上传文件
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping()
    public ResponseEntity<Map<String,Object>> upload(@RequestParam("file")MultipartFile file) throws Exception {
        FileUtils fileUtils=new FileUtils(file.getOriginalFilename(),StringUtils.getFilenameExtension(file.getOriginalFilename()),file.getBytes());
        String[] strings = FdfsUtils.up_load(fileUtils);
        String url="http://47.96.134.170:8888/"+strings[0]+strings[1];
        Map<String ,Object> map=new HashMap<>();
        map.put("url",url);
        return   ResponseEntity.ok(map);
    }
}
